package com.job.Job;
import org.openqa.selenium.By;

public class LoginPage extends BasePage  {
	
	private final String LOGIN_BUTTON = "//div[9]/div/div[1]/div[2]/div[2]/div[2]/div[2]/form/div[7]/input";
	
    /**
     * Default Constructor
     */
    public LoginPage() throws Exception {
        
        try { 
            
            super.init();
            begin();
            
        }
        catch(Exception e) { throw e; }
	
    }
    
    /**
     * Use this constructor from logout
     * @param basePage
     */
    public LoginPage(BasePage basePage) {
		
		inheritSession(basePage);
		begin();
	}

    /**
     * Begin, wait for page to load and perform self-validation.
     */
    protected void begin() {

    	waitForPageToLoad(getTimeout(), false);
    	//waitForElementPresent("name=j_username", getTimeout());
        validate();

    }

    /**
     * Validate that we are indeed on the Login page.
     */
    public void validate() {
       
    	stopPerformanceCapturePageLoaded();        
       // assertElementPresent("Cannot locate Login button.", LOGIN_BUTTON);
		
    }

    /**
     * Perform a login with default username/password (from properties config).     
     * @throws Exception 
     */
    public void login() throws Exception {
    	String username = getUsername();
    	String password = getPassword();

		assertTrue("'username' is not provided in properties config", (username!=null && username.length()>0));
		assertTrue("'password' is not provided in properties config", (password!=null && password.length()>0));
		log.debug("AFTER assertTrue");		
    	login(username, password);
    }
    
    /**
     * Perform a login with a given <code>username</code> and <code>password</code>.
     * 
     * @param username
     * @param password 
     * @throws Exception 
     */
    public void login(String username, String password) throws Exception {

    	typeUsername(username);
    	typePassword(password);
        
        clickLoginButton();
        Thread.sleep(3000);
        driver.navigate().refresh();
   	 
    }
    
    
    
    public void typeUsername(String username) { type(By.xpath("//div[9]/div/div[1]/div[2]/div[2]/div[2]/div[2]/form/div[3]/input"), username); }
    
    public void typePassword(String password) { type(By.xpath("//div[9]/div/div[1]/div[2]/div[2]/div[2]/div[2]/form/div[6]/input"), password); }
    
    /**
     * Click on the Login button.
     */
    public void clickLoginButton() { click(By.xpath(LOGIN_BUTTON)); } 
    	
}
